package com.song.controller.bug;

import com.song.common.Result;
import com.song.controller.BaseController;
import com.song.entity.bug.Bug;
import com.song.service.bug.BugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("${api.version}/bug")
public class BugController  extends BaseController{

    @Autowired
    private BugService bugService;

    /**
     *  Bug管理界面
     */
    @RequestMapping("/bugManage")
    public String bugManage() {
        return "bug/bugManage";
    }

    /**
     * 获取Bug列表
     */
    @RequestMapping("/bugList")
    @ResponseBody
    public Result<List<Bug>> bugList(Bug bug, String startTime, String endTime,
                                       @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        Result<List<Bug>> result  =  bugService.selectBugList(bug,startTime,endTime,pageNum,pageSize);
        return result;
    }
    /**
     *  根据用户ID删除Bug
     */
    @RequestMapping("/deleteBugById")
    @ResponseBody
    public Result deleteBugById(int id) {
        boolean bool = bugService.deleteById(id);
        if (bool) {
            return success("删除Bug成功！");
        } else {
            return error("删除Bug失败！");
        }
    }

    /**
     * 批量删除Bug
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Result<Integer> batchDel(String ids) {
        String[] array = ids.split(",");
        boolean bool =  bugService.deleteBatchIds(Arrays.asList(array));
        if (bool) {
            return success("批量删除Bug成功！");
        } else {
            return error("批量删除Bug失败！");
        }
    }

    /**
     * 新增Bug
     */
    @RequestMapping("/addBug")
    @ResponseBody
    public Result<Integer> addBug(Bug bug) {
        bug.setCreateTime(new Date());
        bug.setCreateAdmin("admin");
        boolean bool = bugService.insert(bug);
        if (bool) {
            return success("新增Bug成功！");
        } else {
            return error("新增Bug失败！");
        }
    }

    /**
     * 更新Bug
     */
    @RequestMapping("/updateBug")
    @ResponseBody
    public Result<Integer> updateBug(Bug bug) {
        boolean bool = bugService.updateById(bug);
        if (bool) {
            return success("更新Bug成功！");
        } else {
            return error("更新Bug失败！");
        }
    }
}
